<template>
	<view class="container">
		<u-loading-page color="#fff" :loading="loading" loading-mode="spinner"></u-loading-page>
		<view class="content-box-header">
			<view class="space-list">
				<view class="space-item">
					<view class="header-box">
						<view class="header-left" @click="spaceMethodURL">
							<view class="header-sculpture-box">
								<img :src="personInfo.weAvatar" alt="" />
							</view>
							<view style="font-size: 40rpx; font-weight: 500">
								{{ personInfo.weUserName }}
							</view>
						</view>
						<view class="header-right">
							<view style="margin-right: 20rpx">
								<view v-if="personInfo.isLike === 'Y'" @click="isLikeMethod('N')"><u-icon size="42rpx"
										color="#e45656" name="heart-fill"></u-icon></view>
								<view v-else @click="isLikeMethod('Y')"><u-icon size="42rpx" name="heart"></u-icon>
								</view>
								<!-- {{ personInfo.likeQuantity }} -->
							</view>
							<view v-if="isCurrentUser" style="margin-right: 20rpx">
								<u-icon @click.stop="delSpaceMethod(personInfo)" size="42rpx" name="trash"></u-icon>
							</view>
							<view v-if="isCurrentUser">
								<u-icon @click.stop="editSpaceMethod(personInfo)" size="42rpx" name="edit-pen"></u-icon>
							</view>
						</view>
					</view>

					<swiper class="swiper" circular :indicator-dots="true" :autoplay="true">
						<swiper-item v-for="url in personInfo.photoList" :key="url">
							<img style="width: 100%; height: 100%; border-radius: 16rpx" :src="url" alt="" srcset="" />
						</swiper-item>
					</swiper>
					<view class="content-title">
						{{ personInfo.title }}
					</view>
					<view class="content-content">
						<u-parse :content="personInfo.content"></u-parse>
					</view>
				</view>
			</view>
		</view>
		<view class="hb-comment">
			<!-- 评价部分 -->
			<view style="padding: 20rpx 30rpx">
				<view class="rich-title">
					<view>评价</view>
					<view style="margin-left: 20rpx"><u-button @click="publishMethod" type="primary" size="mini"
							text="发布"></u-button></view>
				</view>
				<view v-for="item in reviewList" :key="item.id">
					<view class="rich-text">
						<img :src="item.weAvatar" alt="" />
						<view class="title-ruch-box">
							<view style="
                  display: flex;
                  justify-content: space-between;
                  align-items: center;
                ">
								<view>{{ item.weUserName }}
									<text style="margin-left: 20rpx; font-size: 24rpx">
										{{ item.createTime.slice(0, 10) }}
									</text>
								</view>

								<view style="margin-left: 20rpx; float: right;display: flex;">
									<!-- <text style="color: #3c9cff; margin-right: 14rpx"
										@click="recoverMethod(item)">回复</text> -->
									<!-- 	<text v-if="weUserId === item.weUserId" style="color: rgb(243, 52, 52)"
										@click.stop="delMethod(item)">删除</text> -->
									<u-icon @click.stop="recoverMethod(item)" name="plus-circle" color="#216be6"
										size="28"></u-icon>
									<u-icon v-if="weUserId === item.weUserId" @click.stop="delMethod(item)" name="trash"
										color=" rgb(243, 93, 33)" size="28"></u-icon>
								</view>
							</view>
							<view class="ruch-p">{{ item.content }}</view>
						</view>
					</view>
					<template v-if="item.children">
						<view class="children-rich-text" v-for="citem in item.children" :key="citem.id">
							<img :src="citem.weAvatar" alt="" />
							<view class="title-ruch-box">
								<view style="
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                  ">
									<view>{{ citem.weUserName }}
										<text style="margin-left: 20rpx; font-size: 24rpx">
											{{ citem.createTime.slice(0, 10) }}
										</text>
									</view>
									<view style="margin-left: 20rpx; float: right">
										<!-- <text style="color: #3c9cff;" @click="recoverMethod(citem)">回复</text> -->
										<!-- <text v-if="weUserId === citem.weUserId" style="color: rgb(243, 52, 52)"
											@click="delMethod(citem)">删除</text> -->
										<u-icon v-if="weUserId === citem.weUserId" @click.stop="delMethod(citem)"
											name="trash" color=" rgb(243, 93, 33)" size="28"></u-icon>
									</view>
								</view>
								<view class="ruch-p">{{ citem.content }}</view>
							</view>
						</view>
					</template>
				</view>
				<!-- <view class="rich-text">
				<img src="https://cdn.uviewui.com/uview/swiper/swiper1.png" alt="">
				<view class="title-ruch-box">
					<view class="ruch-title">某中客</view>
					<view class="ruch-p">某中客某中客某中客某中客某中客某中客某中客某中客某中客某中客某中客某中客某中客</view>
				</view>
				<view>
					<u-icon name="heart" size="32 rpx"></u-icon>
				</view>
			</view> -->
			</view>

			<!-- 底部 -->
			<u-popup :show="show" @close="close" @open="open" :round="10" mode="bottom">
				<view class="footer-box">
					<view v-if="isPublish" style="padding: 10rpx">
						<text>回复：{{ copy_item.weUserName }}</text>
					</view>
					<view class="footer-box-content">
						<view style="width: 80%">
							<u--input placeholder="评论" shape="circle" prefixIcon="search" v-model="value"
								prefixIconStyle="font-size: 22px;color: #909399"></u--input>
						</view>
						<view style="display: flex; align-items: center">
							<u-button @click="submitMethod" type="primary" size="small" text="发布"></u-button>
						</view>
					</view>
				</view>
			</u-popup>
		</view>
		<u-modal :show="modalShow" :title="title" @confirm="confirmMehtod" @cancel="cancelMethod"
			showCancelButton></u-modal>
	</view>
</template>

<script>
	import {
		commentArea,
		publishComment,
		removeComment,
		unlike,
		like,
		info,
		remove,
	} from "@/api/axios/Space.js";
	// import value from "../../uni_modules/uview-ui/components/u-text/value";
	export default {
		data() {
			return {
				commentData: {},
				personInfo: {},
				reviewList: [],
				isPublish: false,
				show: false,
				openId: "",
				copy_item: {},
				weUserId: "",
				value: "",
				title: "",
				delID: "",
				modalShow: false,
				isCurrentUser: false,
				loading: false,
				id: "",
			};
		},
		methods: {
			delSpaceMethod(item) {
				console.log(item);
				this.title = `是否删除 "${item.title}" ？`;
				this.delID = item.id;
				this.modalShow = true;
			},
			editSpaceMethod(item) {
				uni.redirectTo({
					url: "/pages-space/SpacePublish/index?edit=true&id=" + this.id,
				});
			},
			confirmMehtod() {
				let _this = this;
				remove({
					id: this.delID,
				}).then((res) => {
					_this.modalShow = false;
					uni.navigateBack({
						delta: 1,
					});
					// uni.redirectTo({
					// 	url: "/pages-space/MySpace/index?visitOpenId=" + this.personInfo.openId,
					// });
				});
			},
			cancelMethod() {
				this.modalShow = false;
			},
			spaceMethodURL() {
				console.log(this);
				uni.navigateTo({
					url: "/pages-space/OtherSpace/index?visitOpenId=" + this.personInfo.openId,
				});
			},
			isLikeMethod(isFlag) {
				let _this = this;
				if (isFlag === "Y") {
					like({
						openId: this.openId,
						dynamicId: this.personInfo.id,
					}).then((res) => {
						_this.personInfo.isLike = "Y";
					});
				} else {
					unlike({
						openId: this.openId,
						dynamicId: this.personInfo.id,
					}).then((res) => {
						_this.personInfo.isLike = "N";
					});
				}
				uni.$emit("spaceUpdate");
			},
			submitMethod(item) {
				if (!this.isPublish) {
					let _this = this;
					publishComment({
						openId: this.openId,
						friendsCircleId: this.personInfo.id,
						parentId: "",
						content: this.value,
						receiveWeUserId: "",
					}).then((res) => {
						_this.init(_this.personInfo.id);
						_this.show = false;
						_this.value = "";
					});
				} else {
					let _this = this;
					publishComment({
						openId: this.openId,
						friendsCircleId: this.personInfo.id,
						content: this.value,
						parentId: this.copy_item.id,
						receiveWeUserId: this.copy_item.WeUserId,
					}).then((res) => {
						_this.init(_this.personInfo.id);
						_this.show = false;
						_this.value = "";
					});
				}
			},
			recoverMethod(item) {
				this.show = true;
				this.isPublish = true;
				this.copy_item = item;
			},
			delMethod(item) {
				let _this = this;
				this.loading = true;
				removeComment({
					openId: this.openId,
					dynamicCommentId: item.id,
				}).then((res) => {
					_this.loading = false;
					_this.init(_this.personInfo.id);
				});
			},
			publishMethod() {
				this.isPublish = false;
				this.show = true;
			},
			open() {
				// console.log('open');
			},
			close() {
				this.show = false;
				// console.log('close');
			},
			init(id) {
				let _this = this;
				_this.loading = true;
				commentArea({
					params: {
						id: id,
					},
				}).then((res) => {
					_this.loading = false;
					_this.reviewList = res.content;
				});
			},
		},
		created() {},
		mounted() {},
		onLoad(data) {
			let openId = uni.getStorageSync("openId");
			this.openId = openId;
			let _this = this;
			this.init(data.id);
			this.id = data.id;
			info({
				params: {
					openId,
					id: data.id,
				},
			}).then((res) => {
				console.log(res);
				_this.personInfo = res;
				if (openId === res.openId) {
					_this.isCurrentUser = true;
				} else {
					_this.isCurrentUser = false;
				}
			});
		},
		onReady() {
			this.weUserId = uni.getStorageSync("weUserId");
		},
	};
</script>

<style lang="scss" scoped>
	@import "./index.scss";
</style>